
//
//  DemoViewController.m
//  002
//
//  Created by YYY on 2017/9/19.
//  Copyright © 2017年 成品家（北京）网路科技有限公司. All rights reserved.
//

#import "DemoViewController.h"

@interface DemoViewController ()

//定义block变量
@property(nonatomic,copy) void(^completionBlock)();

@end

@implementation DemoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    /*
     MRC中如果要弱引用对象都是使用assign，不会增加引用计数，但是一旦对象被释放，地址不会改变，继续访问，出现野指针。
     ARC的weak，本质是一个观察者模式，一旦发现对象被释放，会自动将地址设置为nil,更加安全。
     weak的效率会略微差一些。
     */
    //调用函数 block引用self
    __weak typeof(self) weakSelf = self;
    [self test:^{
        
        NSLog(@"%@",weakSelf.view);
    }];
    // Do any additional setup after loading the view.
}

//block函数
-(void)test:(void (^)())complection
{
    //使用属性记录block self引用了block
    self.completionBlock = complection;
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        //睡眠
        [NSThread sleepForTimeInterval:2.0f];
        
        //回到主队列
        dispatch_async(dispatch_get_main_queue(), ^{
            //执行block
            complection();
        });
    });
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
